package cn.wolfcode.service.impl;

import cn.wolfcode.JWTUtils;
import cn.wolfcode.ann.MyLog;
import cn.wolfcode.domain.Emp;
import cn.wolfcode.domain.PageBean;
import cn.wolfcode.domain.Result;
import cn.wolfcode.mapper.EmpMapper;
import cn.wolfcode.oss.OSSUploadUtils;
import cn.wolfcode.service.EmpService;
import cn.wolfcode.web.exception.DataException;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class EmpServiceImpl implements EmpService {

    @Autowired
    private JWTUtils jwtUtils;
    @Autowired
    private OSSUploadUtils ossUploadUtils;
    @Autowired
    private EmpMapper empMapper;
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Integer gender, Date begin, Date end) {
       // 1 设置分页参数
        PageHelper.startPage(page,pageSize);
        List<Emp> list = empMapper.list(name,gender,begin,end);
        Page<Emp> p = (Page<Emp>) list;
        return new PageBean(p.getTotal(),p.getResult());
    }

    @Override
    @MyLog
    public void delList(List<Long> ids) {
        empMapper.delList(ids);
    }

    @Override
    @MyLog
    public void add(Emp emp) {
        emp.setPassword("wolfcode");
        if (StringUtils.isEmpty(emp.getUsername())){
            throw new DataException("用户名不能为空!!");
        }
        emp.setCreateTime(new Date());
        emp.setUpdateTime(new Date());
        empMapper.add(emp);

    }

    @Override
    @MyLog
    public Result empUpLoad(MultipartFile file) throws IOException {
        //进行数据的合理化判断
        /**
         * 判断上传的头像是否是 img 结尾 是否是 图片
         */
        String contentType = file.getContentType();
        log.info("文件的类型:{}",contentType);
        if (contentType!=null && contentType.equals("image/jpeg")){
            //上传
            return Result.success(ossUploadUtils.upload(file)) ;
            //响应url
        }else{
            return Result.error("DSB!上传文件的类型不正确");
        }


    }

    @Override
    public Emp getById(Long id) {
        return empMapper.getById(id);
    }

    @Override
    @MyLog
    public void edit(Emp emp) {
        emp.setUpdateTime(new Date());
        empMapper.edit(emp);
    }

    @Override
    public Result login(Emp emp) {
        Emp emp1 = empMapper.login(emp);
        if (emp1 != null){
            //登录成功
            Map<String,Object> map = new HashMap<>();
            map.put("id",emp1.getId());
            map.put("username",emp1.getUsername());
            String s = jwtUtils.generateJWT(map);
            return Result.success(s);
        }else {
            return Result.error("用户名或密码错误");
        }

    }
}
